﻿package core.ui.controls
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class TextInput extends Sprite
    {
        public var t:TextField;
        private var w:int;
        private var h:int;
        var disable:Sprite;

        public function TextInput(param1:int, param2:int = 0, param3:String = "", param4:int = 20)
        {
            this.w = param1;
            this.h = param4;
            this.graphics.beginFill(9474192);
            this.graphics.drawRect(0, 0, param1, param4);
            this.graphics.beginFill(16777215);
            this.graphics.drawRect(1, 1, param1 - 2, param4 - 2);
            this.graphics.beginBitmapFill(new InputBackground());
            this.graphics.drawRect(1, 1, param1 - 2, param4 > 22 ? (20) : (param4 - 2));
            this.graphics.endFill();
            this.t = new TextField();
            this.t.x = 2;
            this.t.y = 2;
            this.t.defaultTextFormat = new TextFormat("Verdana, Helvetica, San-serif", 11, 3355443);
            this.t.type = TextFieldType.INPUT;
            this.t.maxChars = param2;
            this.t.text = param3;
            this.t.width = param1 - 4;
            this.t.height = param4 - 4;
            this.t.addEventListener(FocusEvent.FOCUS_OUT, this.FocusOut, false, 1, true);
            this.t.addEventListener(FocusEvent.FOCUS_IN, this.FocusIn, false, 1, true);
            addChild(this.t);
            return;
        }// end function

        public function Disable(FocusIn:Boolean = true) : void
        {
            if (this.disable == null)
            {
                this.disable = new Sprite();
                this.disable.visible = false;
                this.disable.alpha = 0.3;
                this.disable.graphics.beginFill(9474192);
                this.disable.graphics.drawRect(0, 0, 10, 10);
                this.disable.graphics.endFill();
                this.disable.height = this.height;
                this.disable.width = this.width;
                addChild(this.disable);
            }
            this.disable.visible = FocusIn;
            if (FocusIn)
            {
                this.t.selectable = false;
                this.t.type = TextFieldType.DYNAMIC;
            }
            else
            {
                this.t.selectable = true;
                this.t.type = TextFieldType.INPUT;
            }
            return;
        }// end function

        private function FocusOut(event:FocusEvent) : void
        {
            AppState.TextInteraction = false;
            return;
        }// end function

        private function FocusIn(event:FocusEvent) : void
        {
            AppState.TextInteraction = true;
            return;
        }// end function

        public function set text(FocusIn:String) : void
        {
            this.t.text = FocusIn;
            return;
        }// end function

        public function get text() : String
        {
            return this.t.text;
        }// end function

        public function set value(FocusIn:String) : void
        {
            this.t.text = FocusIn;
            return;
        }// end function

        public function get value() : String
        {
            return this.t.text;
        }// end function

    }
}
